package com.oig.sys.auth.service.impl;

import com.oig.common.exception.BadRequestException;
import com.oig.sys.auth.dao.ITbJobRoleDao;
import com.oig.sys.auth.dao.ITbRoleDao;
import com.oig.sys.auth.dto.JobDto;
import com.oig.sys.auth.dto.RoleDto;
import com.oig.sys.auth.entity.TbJob;
import com.oig.sys.auth.entity.TbJobRole;
import com.oig.sys.auth.service.ITbJobRoleService;
import com.oig.sys.auth.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbJobRoleServiceImpl implements ITbJobRoleService {

	private final ITbJobRoleDao tbJobRoleDao;
	private final ITbRoleDao roleDao;

	@Autowired
	public TbJobRoleServiceImpl(ITbJobRoleDao tbJobRoleDao,ITbRoleDao roleDao){
		 this.tbJobRoleDao = tbJobRoleDao;
		 this.roleDao = roleDao;
	}


	/**
	 * 少量数据先这么处理
	 */
	@Transactional
	@Override
	public List<RoleVo> addJobRole(TbJob job, List<RoleDto> roleDtoList) {
		List<RoleVo> roleVoList = new ArrayList<>();
		for (RoleDto roleDto : roleDtoList){
			RoleVo roleVo =roleDao.getVoById(roleDto.getId()) ;
			checkData(job, roleVo);
			roleVoList.add(roleVo);
			TbJobRole jobRole = new TbJobRole() ;
			jobRole.setJobId(job.getId());
			jobRole.setRoleId(roleDto.getId());
			this.tbJobRoleDao.save(jobRole) ;
		}
		return roleVoList;
	}


	private void checkData(TbJob job,RoleVo roleVo){
		if (roleVo == null) {
			throw new BadRequestException("角色id错误");
		}
		if (!job.getTenantId().equals(roleVo.getTenantId())){
			throw new BadRequestException("岗位和角色所属租户不同") ;
		}
	}

	@Transactional
	@Override
	public List<RoleVo> updateJobRole(TbJob job, List<RoleDto> roleDtoList) {
		List<RoleVo> roleVoList = new ArrayList<>();
		List<Integer> jobRoleIdList = new ArrayList<>() ;
		for (RoleDto roleDto : roleDtoList) {
			RoleVo roleVo = roleDao.getVoById(roleDto.getId());
			checkData(job, roleVo);
			roleVoList.add(roleVo);
			TbJobRole jobRole = this.tbJobRoleDao.getByRoleIdAndJobId(roleDto.getId(), job.getId()) ;
			if (jobRole==null){
				jobRole = new TbJobRole() ;
				jobRole.setJobId(job.getId());
				jobRole.setRoleId(roleDto.getId());
				this.tbJobRoleDao.save(jobRole) ;
			}
			jobRoleIdList.add(jobRole.getId()) ;
		}
		//如果有索引不用这么麻烦
		List<Integer> delIdList = this.tbJobRoleDao.findAllDelId(job.getId(), jobRoleIdList);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbJobRoleDao.deleteByIdList(delIdList) ;
		}
		return roleVoList;
	}

	@Transactional
	@Override
	public void delJobRoleByRoleId(Integer roleId) {
		List<Integer> delIdList = this.tbJobRoleDao.findAllIdByRoleId(roleId);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbJobRoleDao.deleteByIdList(delIdList) ;
		}
	}

	@Transactional
	@Override
	public void delJobRoleByJobId(Integer jobId) {
		List<Integer> delIdList = this.tbJobRoleDao.findAllIdByJobId(jobId);
		if (delIdList!=null && !delIdList.isEmpty()){
			this.tbJobRoleDao.deleteByIdList(delIdList) ;
		}
	}

	/**
	 * 查询岗位的角色id
	 * @param jobDtoList 岗位列表
	 * @return 角色id列表
	 */
	@Override
	public List<Integer> findAllRoleIdByJob(List<JobDto> jobDtoList) {
		List<Integer> jobIdList = new ArrayList<>();
		for (JobDto jobDto : jobDtoList){
			if (jobDto.getId()==null){
				throw new BadRequestException("岗位id为空");
			}
			jobIdList.add(jobDto.getId());
		}
		return this.tbJobRoleDao.findAllRoleIdByJobIdIn(jobIdList);
	}


	@Override
	public List<RoleVo> getJobRole(Integer jobId) {
		List<Integer> jobIdList = new ArrayList<>();
		jobIdList.add(jobId);
		List<Integer> roleIdList = this.tbJobRoleDao.findAllRoleIdByJobIdIn(jobIdList);
		return this.roleDao.findAllByIdIn(roleIdList);
	}
}
